class CommentsController < ActionController::Base

    unloadable

    before_filter :find_comment, :except => [:create, :new]
    
    def new      
      @comment = Comment.new      
    end

    def create
      @comment = Comment.new(params[:comment])
      
      if @comment.save
         if @comment.approved
          flash[:notice] = t("comment_ok")
         else
          flash[:notice] = t("awaiting_moderation")
         end
         redirect_to :back
      else
         redirect_to :back, :alert => t("fill_out_fields")
      end
    end

    def edit
      if @comment.user_id
        unless @comment.user.id == current_user.id
	        redirect_to :back
        end
      else
        redirect_to :back
      end
      store_location
    end

    def update
      if @comment.update_attributes(params[:comment])
	      redirect_back
      else
	      render :action => :edit
      end
    end

    private

    def find_comment
      @comment = Comment.find(params[:id])
    end

    def store_location
      session[:return_to] = request.referer if request.get?
    end

    def redirect_back
      redirect_to(session[:return_to])
    end

    
  end
